package hedron.dialog;

import hedron.Application;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class PickListDialog extends Dialog {
	private Text string;
	private String message;
	private String[] values;
	private Combo selections;
	private int selection;

	public PickListDialog(Shell parentShell, String message, String[] values, int selection) {
		super(parentShell);
		if (message == null || values == null || values.length <= 0 || selection < 0) {
			throw new IllegalArgumentException();
		}
		this.values = values;
		this.selection = selection;
		this.message = message;
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite grid = new Composite(parent, 0);
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		grid.setLayout(layout);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));

		{// ----------------------------------------------------------
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, false, false);
			gd.horizontalSpan = 3;
			label.setLayoutData(gd);
			label.setText(message);
		}

		{
			string = new Text(grid, SWT.BORDER);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
			gd.widthHint = 350;
			gd.horizontalSpan = 3;
			string.setLayoutData(gd);
			string.setText(message);
			string.setSelection(0, message.length());

			Combo combo = new Combo(grid, SWT.BORDER | SWT.READ_ONLY);
			GridData gdata = new GridData(GridData.FILL_HORIZONTAL);
			gdata.widthHint = 170;
			combo.setLayoutData(gdata);
			combo.setItems(values);
			combo.select(selection);
			selections = combo;
		}
		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.FILL, GridData.BEGINNING, true, false);
			label.setLayoutData(gd);
		}
		{// cancel
			Button button = new Button(grid, SWT.PUSH);
			button.setText("Cancel");
			GridData gd = new GridData(GridData.END, GridData.CENTER, false, false);
			gd.widthHint = 80;
			button.setLayoutData(gd);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setReturnCode(CANCEL);
					close();
				}
			});
		}
		{
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gd = new GridData(GridData.END, GridData.CENTER, false, false);
			gd.widthHint = 80;
			button.setLayoutData(gd);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					setReturnCode(OK);
					selection = selections.getSelectionIndex();
					close();
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}

		grid.pack();
		return grid;
	}

	// We allow the user to resize this dialog
	@Override
	protected boolean isResizable() {
		return true;
	}

	@Override
	public int open() {
		Application.placeDialogInCenter(getParentShell(), getShell());
		return super.open();
	}

	public int getSelectionIndex() {
		return selection;
	}
}
